//
//  InviteGroupMemberViewController.m
//  Findif
//
//  Created by Mac on 2019/3/1.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "InviteGroupMemberViewController.h"
#import "ContactsTableViewHeaderView.h"
#import "GroupMemberTableViewCell.h"
#import "MemberSearchHeaderView.h"
#import "MyAttentionModel.h"

@interface InviteGroupMemberViewController ()<UITableViewDelegate,UITableViewDataSource,MemberSearchHeaderViewDelegate>

@property (nonatomic, weak)UITableView * tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (strong, nonatomic) NSMutableDictionary *dataDic;
@property (nonatomic, strong)NSArray<MyAttentionModel *> * attentionArray;
@property (nonatomic, strong)NSArray<MyAttentionModel *> * fansArray;


@end

@implementation InviteGroupMemberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"邀请新社员";
    self.dataArray = [NSMutableArray array];
    self.dataDic = [NSMutableDictionary dictionary];
    self.attentionArray = [NSArray array];
    self.fansArray = [NSArray array];
    
    [self loadData];
    [self setupUI];
}

- (void)setFansArray:(NSArray<MyAttentionModel *> *)fansArray{
    
    _fansArray = fansArray;
    
    [self.tableView reloadData];
}


- (void)setAttentionArray:(NSArray<MyAttentionModel *> *)attentionArray{
    
    _attentionArray = attentionArray;
    
    [self.tableView reloadData];
}



- (void)loadData{
    
    NSString * user_id = DEF_GET_OBJECT(@"id");
    [[ZRNetworkTool sharedTool] getAttentionUserUser_id:user_id WithSearch:@"" ResultWithBlock:^(BOOL success, id responseData) {
        
        
        if (success) {
            
            self.attentionArray = [NSArray yy_modelArrayWithClass:[MyAttentionModel class] json:responseData[@"data"]];
            
            [self.dataArray addObject:self.attentionArray];
        }else{
            
            [self.dataArray addObject:self.attentionArray];
        }
    }];
    
    [[ZRNetworkTool sharedTool] getFansUserUser_id:user_id WithSearch:@"" ResultWithBlock:^(BOOL success, id responseData) {
        
        //        NSLog(@"model===%@",responseData);
        if (success) {
            
            self.fansArray = [NSArray yy_modelArrayWithClass:[MyAttentionModel class] json:responseData[@"data"]];
            
            [self.dataArray addObject:self.fansArray];
        }else{
            
            [self.dataArray addObject:self.fansArray];
        }
    }];
}

- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topHeight, kScreenW, kScreenH - topHeight - kStatusBarHeight) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF4F5F6);
    tableView.rowHeight = 60 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    MemberSearchHeaderView * headerView =  [[MemberSearchHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 50 * SCREENH6)];
    headerView.delegate = self;
//    headerView.delegate = self;
    // 设置header
    tableView.tableHeaderView = headerView;
    
    self.tableView = tableView;
    
    
    [self.tableView registerClass:[GroupMemberTableViewCell class] forCellReuseIdentifier:@"InviteGroupMemberTableViewCell"];
//    [self.contactsTableView registerClass:[MyAttentionTableViewCell class] forCellReuseIdentifier:@"ChatMyAttentionTableViewCell"];
//    [self.contactsTableView registerClass:[MyFansTableViewCell class] forCellReuseIdentifier:@"ChatMyFansTableViewCell"];
    [self.tableView registerClass:[ContactsTableViewHeaderView class] forHeaderFooterViewReuseIdentifier:@"ContactsTableViewHeaderView"];
    
    //    self.messageTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //
    //        [self updateData];
    //    }];
    
    [self.view addSubview:self.tableView];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    // 取出字典中的 section 如果是第 0 个分区 那么就返回该分区的数据
    if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%ld",section]] isEqualToString:@"0"])
    {
        return [self.dataArray[section] count];
    }else
    {
        //            NSLog(@"**************");
        return 0;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    GroupMemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InviteGroupMemberTableViewCell" forIndexPath:indexPath];
  
    if (indexPath.section == 0 ) {
        
        cell.model = self.attentionArray[indexPath.row];
    }else{
        
        cell.model = self.fansArray[indexPath.row];
    }
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    ContactsTableViewHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ContactsTableViewHeaderView"];
    
    header.userInteractionEnabled = YES;
    header.tag = section;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    [header addGestureRecognizer:tapGR];
    
    switch (section) {
        case 0:
            header.titleLabel.text = @"我的关注";
            header.countLabel.text = [NSString stringWithFormat:@"%lu",self.attentionArray.count];
            break;
        case 1:
            header.titleLabel.text = @"我的粉丝";
            header.countLabel.text = [NSString stringWithFormat:@"%lu",self.fansArray.count];
            break;
        default:
            break;
    }
    
    return header;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * inform_user_id;
    if (indexPath.section == 0) {
       
        MyAttentionModel * model = self.attentionArray[indexPath.row];
        inform_user_id = [NSString stringWithFormat:@"%@",model.attention_user_id];
        
    }else{
        
        MyAttentionModel * model = self.fansArray[indexPath.row];
        inform_user_id = [NSString stringWithFormat:@"%@",model.user_id];
    }
    
    ShowHUD;
    
    NSDictionary * dict = @{@"inform_user_id":inform_user_id,@"group_id":self.group_id};
    
    [[ZRNetworkTool sharedTool] invitationJoinGroupWithDict:dict WithBlock:^(BOOL success, id responseData) {
    
        HiddenHUD;
        if (success) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}


- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    
    if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
        ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = ColorWithRGB(0xF4F5F6);
    }
}


- (void)tapAction:(UITapGestureRecognizer*)sender{
    
    ContactsTableViewHeaderView * header = (ContactsTableViewHeaderView *)sender.view;
    
    //    header.statusImageView.image = [UIImage imageNamed:@"多边形下"];
    NSInteger temp = header.tag;
    if ([header.status isEqualToString:@"show"]) {
        
        header.status = @"hidden";
    }else if ([header.status isEqualToString:@"hidden"]){
        
        header.status = @"show";
    }
    
    //    NSLog(@"temp==%ld",header.tag);
    // 修改 每个区的收缩状态  因为每次点击后对应的状态改变 temp代表是哪个section
    if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%ld",temp]]isEqualToString:@"0"] )
    {
        [self.dataDic setObject:@"1" forKey:[NSString stringWithFormat:@"%ld",temp]];
    }else
    {
        [self.dataDic setObject:@"0" forKey:[NSString stringWithFormat:@"%ld",temp]];
    }
    
    if (temp == 0) {
        
        if (self.attentionArray.count > 0) {
            // 更新 section
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:temp] withRowAnimation:(UITableViewRowAnimationFade)];
        }
    }else if (temp == 1){
        
        if (self.fansArray.count > 0) {
            // 更新 section
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:temp] withRowAnimation:(UITableViewRowAnimationFade)];
        }
    }
}

#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}

- (UIColor *)xy_noDataViewMessageColor {
    
    return [UIColor blackColor];
}

- (void)getSearchString:(NSString *)str{
    
    ShowHUD;
    
    [[ZRNetworkTool sharedTool] searchContactsWithSearch_str:str WithBlock:^(BOOL success, id responseData) {
        
        //        NSLog(@"res===%@",responseData);
        HiddenHUD;
        if (success) {
            
            NSDictionary * dict = responseData[@"data"];
            
            self.attentionArray = [NSArray yy_modelArrayWithClass:[MyAttentionModel class] json:dict[@"attention"]];
            self.fansArray = [NSArray yy_modelArrayWithClass:[MyAttentionModel class] json:dict[@"fans"]];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    }];

}

@end
